<template>
  <div class="DistributionBtn">
    <!-- 分配按钮组件 -->
    <el-dialog
      :visible.sync="dialogVisible"
      :title="title"
      :close-on-click-modal="false"
      :width="show123?'50%':'30%'"
      :show-close="true"
      :before-close="closeDailog"
    >
      <div v-if="show123" class="table-list-box">
        <el-table
          ref="multipleTable"
          v-loading="isLoading"
          tooltip-effect="dark"
          style="width: 100%"
          height="300"
          header-row-class-name="table-list-header"
          :data="tableData"
          @data-change="tableDataChange"
          @selection-change="selectionChange"
        >
          <el-table-column v-if="Number(type) !== 1" type="selection" width="55"></el-table-column>
          <el-table-column v-else label="选择" width="60px" align="center" header-align="center">
            <template slot-scope="scope">
              <el-radio
                v-model="templateRadio"
                style="margin-left: 10px;"
                :label="scope.$index"
                @change.native="getTemplateRow(scope.$index, scope.row)"
              >
                &nbsp;
              </el-radio>
            </template>
          </el-table-column>
          <el-table-column :prop="noLable" :label="text + '编号'"></el-table-column>
          <el-table-column :prop="nameLable" :label="text + '名称'"></el-table-column>
          <el-table-column prop="memberNum" label="成员人数"></el-table-column>
        </el-table>
        <div class="pagination-container">
          <el-pagination
            :current-page.sync="pageInfo.page"
            :page-sizes="[10, 20, 30, 40, 50, 100]"
            :page-size.sync="pageInfo.rows"
            layout="total, sizes, prev, pager, next"
            :total="pageInfo.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          >
          </el-pagination>
        </div>
      </div>
      <FormContainer v-if="type==='4'" :column="1">
        <el-form ref="form" :model="formData" :rules="formRules" label-width="80px">
          <el-form-item prop="seatsName" label="平台工号">
            <el-input v-model="formData.platformNo" placeholder="请输入平台工号" clearable />
          </el-form-item>
        </el-form>
      </FormContainer>

      <FormContainer v-if="type==='5'" :column="1">
        <el-form ref="form5" :model="formDataCallPlatform" :rules="formCallPlatformRules" label-width="80px">
          <el-form-item prop="callPlatform" label="话务平台">
            <SelectForDict
              dict-type-code="CALL_PLATFORM"
              app-code="call-center"
              :code.sync="formDataCallPlatform.callPlatform"
              placeholder="请选择话务平台"
            ></SelectForDict>
          </el-form-item>
        </el-form>
      </FormContainer>
      <span slot="footer" class="dialog-footer">
        <Button :loading="loading" @click="closeDailog">取消</Button>
        <Button type="primary" :loading="loading" @click="confrim">确定</Button>
      </span>
    </el-dialog>
    <Button type="primary" :loading="loading" @click="open">{{ title }}</Button>
  </div>
</template>
<script>
import Button from '@/components/Button'
import FormContainer from '@/components/FormContainer'
import SelectForDict from '@/components/SelectForDict'

import { getDeptNameInfo } from '@/api/department'
import { getBusinessPage } from '@/api/business'
import { getSkillGroupPage } from '@/api/skillGroup'
export default {
  name: 'DistributionBtn',
  components: {
    Button,
    FormContainer,
    SelectForDict
  },
  props: {
    text: {
      type: String,
      default: '1'
    },
    selected: {
      type: Array,
      default: () => ([])
    },
    type: {
      type: String,
      default: ''
    },
    disabled: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      isLoading: false,
      dialogVisible: false,
      loading: false,
      formData: { platformNo: '' },
      formRules: {},
      formDataCallPlatform: {
        callPlatform: ''
      },
      formCallPlatformRules: {
        callPlatform: [
          { required: true, message: '请选择话务平台', trigger: 'change' }
        ]
      },
      tableData: [],
      noLable: '',
      nameLable: '',
      multipleSelection: [],
      oldSelection: [],
      templateSelection: {}, // radio选中对象
      templateRadio: '', // radio是否被选中
      pageInfo: {
        total: 0,
        page: 1,
        rows: 10
      }
    }
  },
  computed: {
    title() {
      return this.text + '分配'
    },
    show123() {
      return ['1', '2', '3', 1, 2, 3].includes(this.type)
    }
  },
  created() {
    // 组织
    if (this.type === '1') {
      this.noLable = 'deptNo'
      this.nameLable = 'deptName'
    }
    // 业务组
    if (this.type === '2') {
      this.noLable = 'groupNo'
      this.nameLable = 'groupName'
    }
    // 技能队列
    if (this.type === '3') {
      this.noLable = 'skillNo'
      this.nameLable = 'skillName'
    }
    // this.tableDataChange()
  },
  methods: {
    closeDailog() {
      // this.multipleSelection = []
      if (this.$refs.multipleTable) this.$refs.multipleTable.clearSelection()
      this.templateSelection = {}
      this.templateRadio = ''
      this.$set(this.formData, 'platformNo', '')
      this.$set(this.formDataCallPlatform, 'callPlatform', '')
      this.dialogVisible = false
    },
    open() {
      if (this.selected.length === 0) {
        this.$message({
          message: '请选择用户',
          type: 'warning'
        })
        return
      }
      this.dialogVisible = true
      this.$nextTick(() => {
        if (Number(this.type) === 4) {
          this.formData.platformNo = this.selected[0].platformNo
        } else {
          this.tableDataChange()
        }
      })
    },
    // 确定
    confrim() {
      if ([5, '5'].includes(this.type)) {
        this.$refs.form5.validate().then(() => {
          this.$emit('confrim', this.formDataCallPlatform.callPlatform, this.selected.map(e => e.id))
          this.closeDailog()
        })
        return
      }
      if (Number(this.type) === 1) {
        this.multipleSelection = []
        this.multipleSelection.push(this.templateSelection)
      }
      this.$emit(
        'confrim',
        this.show123 ? { newArr: this.multipleSelection, oldArr: this.oldSelection } : this.formData
      )
      this.closeDailog()
    },
    // 获取表格请求数据
    tableDataChange() {
      // 组织回显
      const _this = this
      const formData = new FormData()
      formData.append('page', this.pageInfo.page)
      formData.append('rows', this.pageInfo.rows)
      if (Number(_this.type) === 1) {
        _this.isLoading = true
        getDeptNameInfo(formData).then(res => {
          if (res.success === 1) {
            _this.pageInfo.total = res.data.total
            _this.tableData = res.data.records
            if (_this.selected[0].deptNo) {
              const dataList = [...res.data.records]
              const selected = _this.selected[0].deptNo.split(',')
              const oldSelection = []

              selected.forEach(list => {
                dataList.forEach((item, index) => {
                  if (Number(list) === Number(item.deptNo)) {
                    this.$nextTick(() => {
                      oldSelection.push(item)
                      _this.templateSelection = item
                      _this.templateRadio = index
                      // if (_this.$refs.multipleTable) {
                      //   _this.$refs.multipleTable.toggleRowSelection(item, true)
                      // }
                    })
                  }
                })
              })
              _this.$nextTick(() => {
                _this.oldSelection = oldSelection
              })
            }
            _this.isLoading = false
          }
        })
      }
      // 业务回显
      if (Number(this.type) === 2) {
        _this.isLoading = true
        getBusinessPage(formData).then(res => {
          if (res.success === 1) {
            this.pageInfo.total = res.data.total
            this.tableData = res.data.records
            if (_this.selected[0].groupNo) {
              const dataList = [...res.data.records]
              const selected = _this.selected[0].groupNo.split(',')
              const oldSelection = []
              selected.forEach(list => {
                dataList.forEach(item => {
                  if (Number(list) === Number(item.groupNo)) {
                    this.$nextTick(() => {
                      oldSelection.push(item)
                      if (_this.$refs.multipleTable) _this.$refs.multipleTable.toggleRowSelection(item, true)
                    })
                  }
                })
              })
              _this.$nextTick(() => {
                _this.oldSelection = oldSelection
              })
            }
            _this.isLoading = false
          }
        })
      }
      // 技能回显
      if (Number(this.type) === 3) {
        _this.isLoading = true
        getSkillGroupPage(formData).then(res => {
          if (res.success === 1) {
            this.pageInfo.total = res.data.total
            this.tableData = res.data.records
            if (_this.selected[0].skillNo) {
              const dataList = [...res.data.records]
              const selected = _this.selected[0].skillNo.split(',')
              const oldSelection = []
              selected.forEach(list => {
                dataList.forEach(item => {
                  if (Number(list) === Number(item.skillNo)) {
                    this.$nextTick(() => {
                      oldSelection.push(item)
                      if (_this.$refs.multipleTable) _this.$refs.multipleTable.toggleRowSelection(item, true)
                    })
                  }
                })
              })
              _this.$nextTick(() => {
                _this.oldSelection = oldSelection
              })
            }
            _this.isLoading = false
          }
        })
      }
    },
    prevClickPage(val) {
      // console.log(val)
    },
    nextClickPage(val) {},
    // radio选中值
    getTemplateRow(index, row) {
      this.templateSelection = row
    },
    // 多选矿选择时执行
    selectionChange(val) {
      // if (Array.isArray(val) && val.length) {
      this.multipleSelection = val
      // }
    },
    /**
     * 选择每页多少条
     */
    handleSizeChange(pageSize) {
      this.$set(this.pageInfo, 'page', 1)
      this.$set(this.pageInfo, 'rows', pageSize)
      this.$nextTick(() => {
        this.tableDataChange()
      })
    },
    /**
     * 分页
     */
    handleCurrentChange(currrentPage) {
      this.$set(this.pageInfo, 'page', currrentPage)
      this.$nextTick(() => {
        this.tableDataChange()
      })
    }
  }
}
</script>
<style lang='scss' scoped>
</style>
